package rs.etf.mips.crossroad.logic;

import java.io.IOException;
import java.util.EnumMap;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

import rs.etf.mips.crossroad.logic.lightcontrol.LightState;
import rs.etf.mips.crossroad.logic.lightcontrol.LightStateDescriptor;
import rs.etf.mips.crossroad.logic.lightcontrol.LightStateFactory;

public class Crossroad implements ILogicElement{

	private EnumMap<StreetDirection, Street> streets;
	private CrossroadCenter center;
	private LightState lightControl;
	FileHandler handler;
	Logger log;
	
	

	public CrossroadCenter getCenter() {
		return center;
	}

	public Crossroad(double streetLength, double centerSize, double speed) {
		super();
		this.center = new CrossroadCenter(speed, centerSize);
		this.streets = new EnumMap<>(StreetDirection.class);


		Street street = new Street(streetLength, centerSize, speed);
		streets.put(StreetDirection.South, street);
		center.connectStreet(street, StreetDirection.South);
		street.connectToOutOfBounds();

		street = new Street(streetLength, centerSize, speed);
		streets.put(StreetDirection.East, street);
		center.connectStreet(street, StreetDirection.East);
		street.connectToOutOfBounds();

		street = new Street(streetLength, centerSize, speed);
		streets.put(StreetDirection.North, street);
		center.connectStreet(street, StreetDirection.North);
		street.connectToOutOfBounds();

		street = new Street(streetLength, centerSize, speed);
		streets.put(StreetDirection.West, street);
		center.connectStreet(street, StreetDirection.West);
		street.connectToOutOfBounds();
		
		//XXX check
		LightState.setTickPeriod(200);
		this.setLightControl(LightStateFactory.instance(this, 17000).getLightState(LightStateDescriptor.HorizontalForward));
		
		try {
			handler = new FileHandler("rs.etf.mips.crossroad.logic.log");
			log = Logger.getLogger("rs.etf.mips.crossroad.logic.logger");
			log.addHandler(handler);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public LightState getLightControl() {
		return lightControl;
	}

	public void setLightControl(LightState lightControl) {
		this.lightControl = lightControl;
	}

	public OutOfBounds getOutOfBoundsSpace(){
		return OutOfBounds.getInstance();
	}
	
	public Street getStreet(StreetDirection street) {
		return streets.get(street);
	}

	@Override
	public void update() {
	
		if(lightControl!=null){
			lightControl.tick();
		}
		
		center.update();
		for (Street street : streets.values()) {
			street.update();
		}
	}

	public Logger getLog() {
		return log;
	}

	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer("Crossroad []");

		for (StreetDirection direction : streets.keySet()) {
			buffer.append("\n\t"+direction.toString() + ": "+streets.get(direction).toString());
		}

		buffer.append("\n"+this.center);
		buffer.append("\n"+this.getOutOfBoundsSpace());
		return buffer.toString();
	}

	void logMe(){
		if(log!=null)
			log.info(this.toString());
	}
	
	void log(String message){
		if(log!=null)
			log.info(message);
	}
	
	public static void main(String[] args){

		try {
			
			
			Crossroad crossroad = new Crossroad(30, 30, 2);

			for (int i = 0; i < 50; i++) {
				crossroad.update();
				
			}
			
		} catch (Exception e) {
		}
	}

}
